package com.nintendo.frame.edit;

import com.nintendo.entity.pojo.Dish;
import com.nintendo.frame.DishListFrame;
import com.nintendo.mapper.DishDao;
import com.nintendo.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EditDishFrame extends JFrame {

    private JTextField nameField;
    private JTextField codeField;
    private JTextField unitField;
    private JTextField priceField;
    private JTextField statusField;
    private DishListFrame parentFrame;
//    private DishDao dishDao;
    private Dish dish;

    public EditDishFrame(DishListFrame parentFrame, Dish dish) {
        this.parentFrame = parentFrame;
        this.dish = dish;
//        SqlSession sqlSession = MyBatisUtils.getSqlSession();
//        dishDao = sqlSession.getMapper(DishDao.class);
        setTitle("修改菜品");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建面板
        JPanel panel = new JPanel(new GridLayout(6, 2));
        panel.add(new JLabel("名称:"));
        nameField = new JTextField(dish.getName());
        panel.add(nameField);

        panel.add(new JLabel("编码:"));
        codeField = new JTextField(dish.getCode());
        panel.add(codeField);

        panel.add(new JLabel("单位:"));
        unitField = new JTextField(dish.getUnit());
        panel.add(unitField);

        panel.add(new JLabel("价格:"));
        priceField = new JTextField(dish.getPrice().toString());
        panel.add(priceField);

        panel.add(new JLabel("状态:"));
        statusField = new JTextField(dish.getStatus());
        panel.add(statusField);

        JButton editButton = new JButton("修改");
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                editDish();
            }
        });

        panel.add(editButton);

        setContentPane(panel);
    }

    private void editDish() {
        String name = nameField.getText();
        String code = codeField.getText();
        String unit = unitField.getText();
        String priceStr = priceField.getText();
        String status = statusField.getText();

        if (name.isEmpty() || code.isEmpty() || unit.isEmpty() || priceStr.isEmpty() || status.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有字段", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            double price = Double.parseDouble(priceStr);
            dish.setName(name);
            dish.setCode(code);
            dish.setUnit(unit);
            dish.setPrice(price);
            dish.setStatus(status);

            try (SqlSession session = MyBatisUtils.getSqlSession()) {
                DishDao dishDao = session.getMapper(DishDao.class);
                System.out.println("dish====>"+dish);
                dishDao.update(dish);
                session.commit();
                JOptionPane.showMessageDialog(this, "修改成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                dispose();
                parentFrame.refreshTable();
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "价格必须是数字", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new EditDishFrame(null, new Dish()).setVisible(true);
        });
    }
}
